﻿namespace Arms.Framework
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public static class EnumExtensions
    {
        /// <summary>
        /// Converts this type (enum) to dictionary with enum type as key
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static Dictionary<int, string> GetEnumDictionary<T>()
        {
            Dictionary<int, string> dictionary = new Dictionary<int, string>();
            
            foreach (var enumValue in Enum.GetValues(typeof(T)))
            {
                dictionary.Add((int)enumValue, enumValue.ToString());
            }

            return dictionary;
        }
        /// <summary>
        /// Converts this type (enum) to list
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<T> ToEnumList<T>(this T source)
        {
            List<T> list = new List<T>();

            T[] enumValues = Enum.GetValues(typeof(T)) as T[];

            var orderedEnumValues = enumValues.OrderBy(x => x.GetHashCode());

            foreach (var enumValue in orderedEnumValues)
            {
                list.Add(enumValue);
            }

            return list;
        }
    }
}